package com.c.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**

 * 认证成功处理
 */
public class MyAuthenctiationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException {
        Map map = new HashMap();
        map.put("success", true);
        map.put("errorMsg", "登录成功！");
        String str = objectMapper.writeValueAsString(map);

        response.setContentType("text/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(str);
        out.flush();

        // response.sendRedirect("/pages/main.html");//使用ajax无法重定向跳转
        // return;
    }
}
